<template>
  <layout title="首页">
    <!-- 轮播图区域 -->
    <view class="swiper-container">
      <swiper 
        class="swiper" 
        circular 
        :indicator-dots="true" 
        :autoplay="true" 
        :interval="3000" 
        :duration="1000"
        indicator-color="rgba(255, 255, 255, 0.6)"
        indicator-active-color="#ffffff"
      >
        <swiper-item v-for="(item, index) in bannerList" :key="index">
          <view class="swiper-item">
            <image :src="item.image" mode="widthFix" class="swiper-image" @click="handleBannerClick(item)"/>
          </view>
        </swiper-item>
      </swiper>
    </view>

    <view class="home-container">
      <!-- 欢迎区域 -->
      <view class="header">
        <text class="welcome">欢迎回来</text>
        <view class="user-info">
          <image 
            :src="userInfo.image" 
            class="avatar" 
            mode="aspectFill"
          />
          <text class="username">{{ userInfo.name }}</text>
        </view>
      </view>

      <!-- 功能区域 -->
      <view class="content">
        <uni-card class="card" :border="false" :shadow="false">
          <view class="card-header">
            <text class="card-title">快捷功能</text>
          </view>
          <view class="grid">
            <view class="grid-item" v-for="(item, index) in quickActions" :key="index" @click="handleAction(item)">
              <text class="grid-icon">{{ item.icon }}</text>
              <text class="grid-text">{{ item.name }}</text>
            </view>
          </view>
        </uni-card>
      </view>
      <!-- 今日推荐模块 -->
      <view class="recommend-container">
        <view class="recommend-header">
          <text class="recommend-title">今日推荐</text>
        </view>
        <view class="recommend-list">
          <view class="recommend-item" v-for="(item, index) in recommendNews" :key="index" @click="navigateToDetail(item.id)">   
            <image :src="item.image1" mode="aspectFill" class="recommend-image" />
            <view class="recommend-info">
              <text class="recommend-title">{{ item.summary }}</text>
              <view class="recommend-meta">
                <view class="meta-item">
                  <text class="meta-icon">🤍</text>
                  <text class="meta-text">{{ item.likes || 0 }}</text>
                </view>
                <view class="meta-item">
                  <text class="meta-icon">💬</text>
                  <text class="meta-text">{{ item.comments || 0 }}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </layout>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Layout from '@/layout/index.vue'
import user from '@/api/user'
import { useUserStore } from '@/store/user'
import news from '@/api/news'

const userStore = useUserStore()
const recommendNews = ref([])
const username = ref('')
const userInfo = ref({
  username: '',
  name: '',
  password: '',
  image: '',
  id: 0
})
const quickActions = ref([
  { name: '消防新闻', icon: '📊', action: 'function1',path:'/pages/News/index' },
  { name: '消防知识竞赛', icon: '📝', action: 'function2',path:'/pages/DivideAndConquer/index' },
  { name: 'Ar消防逃生', icon: '📅', action: 'function3',path:'/pages/arCamera/index' },
  { name: '消防知识学习', icon: '⚙️', action: 'function4' }
])

const bannerList = ref([
  { image: '../../static/lunboimage/1.png', id: 1, title: '轮播图1' },
  { image: '../../static/lunboimage/2.png', id: 2, title: '轮播图2' },
  { image: '../../static/lunboimage/3.png', id: 3, title: '轮播图3' },
  { image: '../../static/lunboimage/4.png', id: 4, title: '轮播图4' }
])

onMounted(() => {
  const token = userStore.token
  if (!token) {
    uni.redirectTo({ url: '/pages/index/login' })
  }
  userInfo.value = userStore.userInfo
  getRecommendNews()
})

const navigateToDetail = (id) => {
  uni.navigateTo({
    url: `/pages/News/NewsDetail/index?id=${id}`
  })
}

const handleAction = (item) => {
  uni.navigateTo({
    url: item.path
  })
}

// 获取推荐新闻
const getRecommendNews = async () => {
  try {
    const res = await news.getNewsList()
    if (res.code === 100200) {
      recommendNews.value = res.data.slice(0, 3)
      console.log(recommendNews.value)
    }
  } catch (error) {
    console.error('获取推荐新闻失败:', error)
  }
}

const handleBannerClick = (item) => {
  uni.showToast({
    title: `点击了${item.title}`,
    icon: 'none'
  })
}

</script>

<style scoped>
/* 轮播图样式 */
.swiper-container {
  padding: 0 15px;
}

.swiper {
  width: 100%;
  height: 480rpx;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.swiper-item {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}

.swiper-image {
  width: 100%;
  height: 480rpx;
  display: block;
  object-fit: cover;
}

/* 主容器样式 */
.home-container {
  padding: 15px;
  padding-top: 10px;
}

/* 欢迎区域样式 */
.header {
  background: linear-gradient(120deg, #f6d365 0%, #fda085 100%);
  padding: 20px;
  border-radius: 16px;
  margin: 15px 0;
  box-shadow: 0 4px 12px rgba(253, 160, 133, 0.2);
}

.welcome {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 8px;
  display: block;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.8);
}

.username {
  font-size: 20px;
  color: #fff;
  font-weight: bold;
}

/* 卡片样式 */
.card {
  background: #fff;
  border-radius: 16px;
  margin-top: 15px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.card-header {
  padding: 15px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

/* 网格样式 */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  padding: 12px;
}

.grid-item {
  background: #f8f8f8;
  border-radius: 12px;
  padding: 16px;
  text-align: center;
  transition: all 0.3s;
}

.grid-item:active {
  background: #f0f0f0;
  transform: scale(0.98);
}

.grid-icon {
  font-size: 24px;
  margin-bottom: 6px;
  display: block;
}

.grid-text {
  font-size: 14px;
  color: #666;
}

.recommend-container {
  margin-top: 20px;
}

.recommend-header {
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.recommend-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.recommend-list {
  padding: 10px;
}

.recommend-item {
  display: flex;
  align-items: flex-start;
  padding: 12px;
  border-bottom: 1px solid #f5f5f5;
  gap: 12px;
}

.recommend-image {
  width: 100px;  /* 减小图片宽度 */
  height: 75px;  /* 减小图片高度 */
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
}

.recommend-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 75px;  /* 与图片高度保持一致 */
}

.recommend-info .recommend-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  line-height: 1.4;
  margin-bottom: 8px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.recommend-meta {
  display: flex;
  gap: 15px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.meta-icon {
  font-size: 14px;
}

.meta-text {
  font-size: 12px;
  color: #999;
}
</style> 